package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.Airline;
import com.yoyo.itravel.bean.PageBean;
import com.yoyo.itravel.dao.IAirlineDao;
import com.yoyo.itravel.exceptions.AirlineException;
import com.yoyo.itravel.service.IAirlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

/**
 * @author liuchaojie
 * @version 1.0
 * @Description 航空公司操作服务层接口实现类【服务层实现类】
 * @date 2020/6/18 16:04
 */
@Service
@Transactional
public class AirlineServiceImpl implements IAirlineService {

    @Autowired
    private IAirlineDao airlineDao;

    /**
     * 查询所有航空公司
     * @return
     * @throws AirlineException
     */
    @Override
    public List<Airline> selectAll() throws AirlineException {

        try {
                return airlineDao.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new AirlineException("查询所有航空公司失败！...");
        }
    }

    /**
     * 根据airlineId查找航空公司
     * @param airlineId
     * @return
     * @throws AirlineException
     */
    @Override
    public Airline selectByAirlineId(String airlineId) throws AirlineException {
        if(null == airlineId){
            throw new AirlineException("传入的airlinedId为空！...");
        }else{
            Airline airline = null;
            try {
                airline = airlineDao.selectByAirlineId(airlineId);
            } catch (SQLException e) {
                e.printStackTrace();
                throw new AirlineException("根据airlineId查找航空公司失败！...");
            }
            return airline;
        }
    }

    /**
     * 增加航空公司
     * @param airline
     * @return
     * @throws AirlineException
     */
    @Override
    public boolean addAirline(Airline airline) throws AirlineException {
        try {
            boolean isAddSuccess = airlineDao.insert(airline);
            return isAddSuccess;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new AirlineException("新增航空公司信息失败！...");
        }
    }


    /**
     * 根据airlineId删除航空公司
     * @param airlineId
     * @return
     * @throws AirlineException
     */
    @Override
    public boolean deleteByAirlineId(String airlineId) throws AirlineException {
        try {
            if(null == airlineId){
                throw new AirlineException("airlineId为空，删除失败！...");
            }else{
                boolean isDelSuccess = airlineDao.deleteByAirlineId(airlineId);
                return isDelSuccess;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new AirlineException("根据airlineId删除航空公司信息失败！...");
        }
    }
    /**
     * 修改航空公司信息
     * @param airline
     * @return
     * @throws AirlineException
     */
    @Override
    public boolean changeAirline(Airline airline) throws AirlineException {
        if(null == airline){
            throw new AirlineException("传入的航天公司信息为空！...");
        }else {
            try {
                boolean flag = 1 == airlineDao.change(airline);
                return flag;
            } catch (SQLException e) {
                e.printStackTrace();
                throw new AirlineException("修改航空公司信息失败！...");
            }
        }

    }


    /**
     * 根据航空公司id修改准点率
     * @param airlineId
     * @param ontimeRate
     * @return
     * @throws AirlineException
     */
    @Override
    public boolean changeOnTimeRate(String airlineId, Double ontimeRate) throws AirlineException {
        if(null == airlineId){
            throw new AirlineException("航空公司id为空！...");
        }
        try {
            boolean isSuccess  =  1 == airlineDao.changeOnTimeRate(airlineId, ontimeRate);//数据库修改成功，则更新Redis

            return isSuccess;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new AirlineException("根据航空公司id修改准点率失败！...");
        }
    }


    /**
     * 分页查询
     * @param pageBean
     * @return
     * @throws AirlineException
     */
    @Override
    public List<Airline> findAirlineByPage(PageBean pageBean) throws AirlineException {
        try {
            int startPage = pageBean.getStartPage();
            int pageSize = pageBean.getPageSize();
            int i = (startPage - 1) * pageSize;
            pageBean.setStartPage(i);
            return airlineDao.selectAirlineByPage(pageBean);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new AirlineException("航空公司分页查询失败！...");
        }
    }

    /**
     * 查询总数
     * @return
     * @throws AirlineException
     */
    @Override
    public Integer totalCount() throws AirlineException {
        try {
            return airlineDao.totalCount();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new AirlineException("航空公司查询总数失败！...");
        }
    }

}
